import { DefaultOptionType } from "antd/lib/select";
import { areaList } from "src/asset/data/area";
export const getAreaTreeData = () => {
	const { province_list, city_list, county_list } = areaList;
	const formatList: (
		areaObj: Record<number, string>,
		selectable?: boolean
	) => DefaultOptionType[] = (areaObj, selectable = true) => {
		return Object.entries(areaObj).map(([key, value]) => ({
			label: value,
			value,
			code: key,
			selectable,
		}));
	};
	const province = formatList(province_list, false);
	const city = formatList(city_list, false);
	const country = formatList(county_list);
	province.forEach((provinceItem) => {
		const { code } = provinceItem;
		if (code.substring(2) === "0000") {
			provinceItem.children = [];
			for (let cityIndex = 0; cityIndex < city.length; cityIndex++) {
				const cityItem = city[cityIndex];
				const { code: code1 } = cityItem;
				if (code1.substring(0, 2) === code.substring(0, 2)) {
					provinceItem.children?.push({
						...cityItem,
						value: `${provinceItem.label} ${cityItem.label}`,
						children: [],
					});
					city.splice(cityIndex, 1);
					cityIndex--;
					for (
						let countryIndex = 0;
						countryIndex < country.length;
						countryIndex++
					) {
						const countryItem = country[countryIndex];
						const { code: code2 } = countryItem;
						const str2 = code2.substring(0, 4);
						const str1 = code1.substring(0, 4);
						if (str2 === str1) {
							provinceItem.children?.[
								provinceItem.children.length - 1
							].children.push({
								...countryItem,
								value: `${provinceItem.label} ${cityItem.label} ${countryItem.label}`,
							});
							country.splice(countryIndex, 1);
							countryIndex--;
						}
					}
				}
			}
		}
	});
	return province;
};
export type SpecifiedLengthArrayType = {
	array: unknown[];
	max: number;
	length: number;
};
export class SpecifiedLengthArray {
	array: unknown[] = [];
	max: number;
	length = 0;
	constructor(items = [], options: { maxLength: number }) {
		const { maxLength } = options;
		this.max = maxLength;
		if (this.max < items.length) {
			this.array = items.slice(0, this.max);
			this.length = this.max;
		} else {
			this.array = [...items];
			this.length = items.length;
		}
		return this;
	}
	push(item: unknown): number {
		this.array = [...this.array, item];
		let length = this.array.length;
		if (length > this.max) {
			this.array = this.array.slice(length - 1 - this.max + 1);
			length = this.max;
		}
		this.length = length;
		return length;
	}
	unshift(item: unknown): number {
		this.array = [item, ...this.array];
		let length = this.array.length;
		if (length > this.max) {
			this.array = this.array.slice(0, this.max);
			length = this.max;
		}
		this.length = length;
		return length;
	}
}
